package com.ls.lserp.modules.sys.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lserp.common.mapper.JsonMapper;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.Constants;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.sys.dao.DesignoptionDao;
import com.ls.lserp.modules.sys.dao.FitpropertyDao;
import com.ls.lserp.modules.sys.entity.DesignoptionCache;
import com.ls.lserp.modules.sys.entity.FitpropertyCache;

import java.util.List;
import java.util.Map;

/**
 * 设计项目工具类
 *
 * @author 于辉
 * @version 2017-06-16
 */
public class FitpropertyUtils {

	private static FitpropertyDao fitpropertyDao = SpringContextHolder.getBean(FitpropertyDao.class);

	public static final String CACHE_FITPROPERTY_MAP = "fitpropertyMap";

	public static final String CACHE_FITPROPERTY_VALUE_MAP = "fitpropertyValueMap";

	/**
	 * @Title: putFitPropertyCache
	 * @Description: 服装款式设计项目全量放入缓存
	 * @author 于辉
	 *
	 */
	public static void putFitPropertyCache() {
		@SuppressWarnings("unchecked")
		Map<String, List<FitpropertyCache>> fitpropertyMap = (Map<String, List<FitpropertyCache>>) CacheUtils.get(CACHE_FITPROPERTY_MAP);
		if (fitpropertyMap == null) {
			fitpropertyMap = Maps.newHashMap();
			for (FitpropertyCache fitpropertyCache : fitpropertyDao.findAllList(new FitpropertyCache())) {
				List<FitpropertyCache> dictList = fitpropertyMap.get(fitpropertyCache.getItemValue ());
				if (dictList != null) {
					dictList.add(fitpropertyCache);
					fitpropertyMap.put(fitpropertyCache.getItemValue(), Lists.newArrayList(dictList));
				} else {
					fitpropertyMap.put(fitpropertyCache.getItemValue(), Lists.newArrayList(fitpropertyCache));
				}

			}
			CacheUtils.put(CACHE_FITPROPERTY_MAP, fitpropertyMap);
		}
	}

	/**
	 * @Title: getFitPropertyList
	 * @Description: 返回量体项目列表
	 * @author 于辉
	 *
	 * @param code
	 * @return List<FitpropertyCache>
	 *
	 */

	public static List<FitpropertyCache> getFitPropertyList(String code) {

		@SuppressWarnings("unchecked")
		Map<String, List<FitpropertyCache>> fitpropertyMap = (Map<String, List<FitpropertyCache>>) CacheUtils.get(CACHE_FITPROPERTY_MAP);
		if (fitpropertyMap == null) {
			fitpropertyMap = Maps.newHashMap();
			for (FitpropertyCache fitpropertyCache : fitpropertyDao.findAllList(new FitpropertyCache())) {
				List<FitpropertyCache> fitList = fitpropertyMap.get(fitpropertyCache.getItemCode());
				if (fitList != null) {
					fitList.add(fitpropertyCache);
				} else {
					fitpropertyMap.put(fitpropertyCache.getItemCode(), Lists.newArrayList(fitpropertyCache));
				}
			}
			CacheUtils.put(CACHE_FITPROPERTY_MAP, fitpropertyMap);
		}
		List<FitpropertyCache> fitList = fitpropertyMap.get(code);

		if (fitList == null) {
			fitList = Lists.newArrayList();
		} else {
			for (FitpropertyCache fit : fitList) {
				if (Constants.EN_US.equals(StringUtils.getLanguage())) {
					fit.setItemName(fit.getItemNameEn());
				} else {
					fit.setItemName(fit.getItemNameCn());
				}
			}
		}

		return fitList;
	}

	/**
	 * @Title: getFitPropertyEntity
	 * @Description: 量体项目Entity
	 * @author 于辉
	 *
	 * @param value
	 * @return FitpropertyCache
	 *
	 * @return List<FitpropertyCache>
	 */

	public static FitpropertyCache getFitPropertyEntity(String value) {

		Map<String, FitpropertyCache> fitValueMap = (Map<String, FitpropertyCache>) CacheUtils.get(CACHE_FITPROPERTY_VALUE_MAP);

		if (fitValueMap == null) {

			fitValueMap = Maps.newHashMap();
			for (FitpropertyCache designoption : fitpropertyDao.findAllList(new FitpropertyCache())) {
				fitValueMap.put(designoption.getItemValue(), designoption);
			}
			CacheUtils.put(CACHE_FITPROPERTY_VALUE_MAP, fitValueMap);

		}

		FitpropertyCache returnDesignoption = fitValueMap.get(value);

		if (returnDesignoption != null) {
			return returnDesignoption;
		} else {
			return new FitpropertyCache();
		}

	}

	/**
	 * 返回量体项目名称
	 *
	 * @param value
	 * @return
	 */
	public static String getFitPropertyName(String value) {

		Map<String, FitpropertyCache> fitValueMap = (Map<String, FitpropertyCache>) CacheUtils.get(CACHE_FITPROPERTY_VALUE_MAP);

		if (fitValueMap == null) {

			fitValueMap = Maps.newHashMap();
			for (FitpropertyCache fitpropertyCache : fitpropertyDao.findAllList(new FitpropertyCache())) {
				fitValueMap.put(fitpropertyCache.getItemValue(), fitpropertyCache);
			}
			CacheUtils.put(CACHE_FITPROPERTY_VALUE_MAP, fitValueMap);

		}

		FitpropertyCache returnFitproperty = fitValueMap.get(value);

		if (returnFitproperty != null) {
			if (Constants.EN_US.equals(StringUtils.getLanguage())) {
				return returnFitproperty.getItemNameEn();
			} else {
				return returnFitproperty.getItemNameCn();
			}
		} else {
			return "";
		}
	}
	/**
	 * @Title: getFitPropertyListJson
	 * @Description: 返回量体项目列表（JSON）
	 * @author 于辉
	 *
	 * @param code
	 * @return String
	 */
	public static String getFitPropertyListJson(String code) {
		return JsonMapper.toJsonString(getFitPropertyList(code));
	}

}
